module ReportHelper
  
  def show_option(option, answer)
      state = answer.state_for(option)
      yes_checked = state
      no_checked = (not state.nil? and not state)
      code = option.is_other ? '' : option.code.downcase + '.'
      
      ret = radio_button_tag("answers_options[#{answer.id}][#{option.id}][state]", 1, yes_checked, :class => "yes #{'other' if option.is_other}")
      ret << _(' Yes ')
      ret << radio_button_tag("answers_options[#{answer.id}][#{option.id}][state]", 0, no_checked, :class => "no #{'other' if option.is_other}")
      ret << _(' No ')
      
      ret << '----> ' + code + _(option.text)
      
      if option.is_other   
        ret << ' ' + text_field_tag("answers_options[#{answer.id}][#{option.id}][value_text]", answer.value_text, :class => "other_text #{'hidden' if state.nil? or not state}", :size => 50)  
        ret << error_message_on(answer, :value_text) if option.is_other
      end
      
      ret << check_box_tag("answers_options[#{answer.id}][#{option.id}][_delete]", 1, state.nil?, :class => 'deleteanswer hidden')
  end
  
  def show_yes_no_answer(answer)
    if answer.is_yes
      '<img src="/images/circ_y.gif" />'
    elsif answer.is_no
      '<img src="/images/circ_n.gif" />'
    else
      '-'
    end
  end
  
  def show_options_answer(answer, option)
    state = answer.state_for option
    show_yes = state
    show_no = (not state.nil? and not state)
    if show_yes
      '<img src="/images/circ_y.gif" />'
    elsif show_no
      '<img src="/images/circ_n.gif" />'
    else
      '--'
    end
  end
  
  def percentage(number)
    # force something less than 100% when it is not completed
    res = '%.0f%' % (number * 100)
    if res == '100%' && number < 1
      res = '99%'
    end

    res
  end
  
  class ActionView::Helpers::FormBuilder
    
    def yes_no_question(yes, no, answer)
      yes_no_options(yes, no) + yes_no_clarification(yes, no, answer)
    end
    
    def yes_no_clarification(yes, no, answer)
      return if yes.text.blank? or no.text.blank? or answer.nil?
      ret = self.label(:value_text, _(yes.text), :class => "yes clarification #{'hidden' unless answer.is_yes }") 
      ret << self.label(:value_text, _(no.text), :class => "no clarification #{'hidden' unless answer.is_no}")
      ret << self.text_field(:value_text, :class => "clarificationtext #{'hidden' if answer.empty?}", :size => 50)
      ret << error_message_on( :value_text, answer)
      ret
    end
        
    def yes_no_options(yes, no)
      yes_no_option(yes, _('Yes')) + ' ' + yes_no_option(no, _('No')) 
    end
    
    def answer_hidden_fields
      ret = self.check_box '_delete', :class => 'deleteanswer hidden', :checked => @object.empty?
      ret << self.hidden_field(:id) 
      ret << self.hidden_field(:report_response_id) 
      ret << self.hidden_field(:question_id)
      ret << self.hidden_field(:site_id)
      ret      
    end
    
    private
    
    def yes_no_option(option, text=nil)
      text ||= option.code.capitalize
      self.radio_button('option_id', option.id, :class => text.downcase) + ' ' + self.label('option_id', text, :value => option.id)
    end
    
  end

end
